package org.mao.forum.job;

import java.util.Date;
import java.util.concurrent.ScheduledFuture;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.mao.forum.entity.ForumTask;
import org.mao.forum.enums.ForumTaskEnum.ForumTaskFrequencyEnum;
import org.mao.forum.enums.ForumTaskEnum.ForumTaskStatusEnum;
import org.mao.forum.service.ForumTaskService;
import org.mao.forum.vo.ForumTaskParamsVo;
import org.mao.parser.ForumParserProxy;
import org.mao.quartz.QuartzTaskSchedule;

import com.google.gson.Gson;

public class GrabPostingImageJob implements Runnable {

	private static Logger logger = Logger.getLogger(GrabPostingImageJob.class);

	private Integer taskId;

	private ForumTask forumTask = null;

	private ForumTaskParamsVo paramsVo = null;

	private Integer count = null;

	private ForumTaskService forumTaskService;
	
	private ForumParserProxy forumParserProxy;

	public GrabPostingImageJob(Integer taskId, ForumTaskService forumTaskService, ForumParserProxy forumParserProxy) {
		this.taskId = taskId;
		this.forumTaskService = forumTaskService;
		this.forumParserProxy = forumParserProxy;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		logger.info("start job " + this + "[taskId=" + this.taskId + "]");
		try {
			Date now = new Date();
			// 第一次查询得到
			if (this.forumTask == null) {
				this.forumTask = new ForumTask();
				this.forumTask.setId(this.taskId);
				this.forumTask = forumTaskService.get(this.forumTask);
				this.count = this.forumTask.getCount();
				if (this.count == null) {
					this.count = 0;
				}
				// 发现任务的状态不是正在执行，则更新为正在执行
				if (!ForumTaskStatusEnum.EXECUTING.equals(this.forumTask.getStatus())) {
					this.forumTask.setStatus(ForumTaskStatusEnum.EXECUTING.getStatus());
					this.forumTask.setUpdateTime(now);
					forumTaskService.update(this.forumTask);
				}
				String paramsStr = this.forumTask.getParams();
				if (StringUtils.isNotBlank(paramsStr)) {
					Gson gson = new Gson();
					this.paramsVo = gson.fromJson(paramsStr, ForumTaskParamsVo.class);
				}
			}

			logger.info("start execute job[name=" + this.forumTask.getName() + "] ");
			forumParserProxy.scan(this.forumTask, this.paramsVo);
			logger.info("end execute job[name=" + this.forumTask.getName() + "] ");

			now = new Date();
			this.count++;
			this.forumTask.setUpdateTime(now);
			this.forumTask.setCount(this.count);
			if (ForumTaskFrequencyEnum.ONCE.equals(this.forumTask.getType()) || (ForumTaskFrequencyEnum.INTELVAL.equals(this.forumTask.getType()) && this.forumTask.getEndTime().before(now))) {
				boolean cancel = true;
				if (ForumTaskFrequencyEnum.INTELVAL.equals(this.forumTask.getType())) {
					ScheduledFuture<?> scheduledFuture = QuartzTaskSchedule.forumTaskUniqueList.get(this.taskId);
					cancel = scheduledFuture.cancel(false);
					logger.info("cancel job " + this + "[taskId=" + this.taskId + "][cancel=" + cancel + "]");
				}
				if (cancel) {
					this.forumTask.setStatus(ForumTaskStatusEnum.OVER.getStatus());
					this.forumTask.setUpdateTime(now);
					QuartzTaskSchedule.forumTaskUniqueList.remove(this.taskId);
				}
			}
			forumTaskService.update(this.forumTask);
			logger.info("end job " + this + "[taskId=" + this.taskId + "]");
		} catch (Exception e) {
			logger.error("", e);
		}
	}

}
